package com.niit.onlinemusicserver34.controller;

import com.niit.onlinemusicserver34.common.R;
import com.niit.onlinemusicserver34.constant.Constants;
import com.niit.onlinemusicserver34.model.domain.Singer;
import com.niit.onlinemusicserver34.model.request.SingerRequest;
import com.niit.onlinemusicserver34.service.SingerService;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/singer")
public class SingerController {

    @Autowired
    SingerService singerService;

    @RequestMapping("/list")
    public R list(){
        return singerService.listAll();
    }
    @RequestMapping("/listbySex")
    public R listBySex(Byte sex){
        return singerService.getBySex(sex);
    }
    @PostMapping("/insert")
    public R list(@RequestBody  SingerRequest singerRequest){
        return singerService.saveSinger(singerRequest);
    }

    @DeleteMapping("/delete")
    public R list(@RequestParam("id") Integer id){
        return singerService.deleteSinger(id);
    }

    @PostMapping("/update")
    public R update(@RequestBody  SingerRequest singerRequest){
        return singerService.updateSinger(singerRequest);
    }

    @PostMapping("/avatar/update")
    public R updatePic(MultipartFile file, Integer id){

        String sep = System.getProperty("file.separator");
        String newFileName = System.currentTimeMillis()+"_"+file.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + sep + "img" + sep + "singerPic"+sep;
        try {
            file.transferTo(new File(filePath+newFileName));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        Singer singer = new Singer();
        singer.setPic("img" + sep + "singerPic"+sep+newFileName);
        singer.setId(id);
        singerService.updateById(singer);

        return R.success("更新成功");
    }
}
